﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Expectation_Maximization
{
	public static class HelperFunctions
	{

		public struct Sequence
		{
			public string seq;
			public int motifStart;
		}

		/// <summary>
		/// Gets a number from the user via command line. Returns null if no number is entered
		/// </summary>
		/// <returns></returns>
		public static Nullable<int> GetNumericInput()
		{
			string input;
			int result;

			while (true)
			{
				input = Console.ReadLine();
				if (input == "")
					return null;
				else if (int.TryParse(input, out result))
					return result;
				else
					Console.Write("Must be a number! Try again: ");
			}

		}

		/// <summary>
		/// Reads in multiple sequences from the passed file name. Returns an array of strings containing the sequences
		/// </summary>
		/// <param name="filename"></param>
		/// <returns></returns>
		public static List<HelperFunctions.Sequence> ImportSequence(string filename)
		{
			List<HelperFunctions.Sequence> sequences = new List<HelperFunctions.Sequence>();

			System.IO.StreamReader sr = new System.IO.StreamReader(filename);

			string line = sr.ReadLine();

			while (line != null)
			{
				//if line starts with ">" then the next line in the file will contain the sequence. Add that shit to the list
				if (line.StartsWith(">"))
				{
					Sequence seq = new Sequence();
					seq.seq = sr.ReadLine();
					sequences.Add(seq);
				}

				line = sr.ReadLine();
			}

			return sequences;
		}
	}
}
